package com.bytech.user.biz.service.blogUser.Impl;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.entity.blogUser.BlogUser;
import com.bytech.common.biz.entity.blogUser.BlogUserAgent;
import com.bytech.common.biz.entity.blogUser.BlogUserLoginDto;
import com.bytech.common.biz.enums.CommonStatusEnum;
import com.bytech.common.config.AppGlobalConstants;
import com.bytech.common.utils.ApplicationBeanUtils;
import com.bytech.common.utils.JedisClient;
import com.bytech.common.utils.StringUtils;
import com.bytech.user.biz.mapper.blogUser.BlogUserMapper;
import com.bytech.user.biz.service.blogUser.BlogUserService;
import com.bytech.common.biz.service.base.BasicServiceImpl;
import com.google.common.collect.Lists;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * @Author Zhao
 * @Create 2021-07-26
 */
@Service
public class BlogUserServiceImpl extends BasicServiceImpl<BlogUserMapper, BlogUser> implements BlogUserService {

    @Resource
    private BlogUserMapper blogUserMapper;
    @Resource
    private JedisClient jedisClient;

    @Override
    public BlogUser getByToken(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        LambdaQueryWrapper<BlogUser> userQueryWrapper = new QueryWrapper<BlogUser>().lambda();
        userQueryWrapper.eq(BlogUser::getToken, token);
        return blogUserMapper.selectOne(userQueryWrapper);
    }

    @Override
    public List<BlogUser> getByIds(Set<String> userIds) {
        if (CollectionUtils.isEmpty(userIds)) {
            return Lists.newArrayList();
        }
        return blogUserMapper.selectBatchIds(userIds);
    }

    @Override
    @Transactional
    public BaseResult<BlogUser> login(BlogUserLoginDto loginDto) {
        LambdaQueryWrapper<BlogUser> userQueryWrapper = new QueryWrapper<BlogUser>().lambda();
        userQueryWrapper.eq(BlogUser::getAccount, loginDto.getAccount());
        BlogUser blogUser = blogUserMapper.selectOne(userQueryWrapper);
        if (blogUser == null) {
            return BaseResult.<BlogUser>error("用户不存在").data(null).build();
        }
        String md5Pwd = StringUtils.strToMd5(loginDto.getPassword() + AppGlobalConstants.BLOG_PASSWORD_KEY);
        if (!blogUser.getPassword().equalsIgnoreCase(md5Pwd)) {
            return BaseResult.<BlogUser>error("密码错误").data(null).build();
        }
        // 登录成功
        if (!StringUtils.isBlank(loginDto.getLoginIp())) {
            blogUser.setLastLoginIp(loginDto.getLoginIp());
        }
        blogUser.setLastLoginTime(new Date());
        blogUserMapper.updateById(blogUser);
        return BaseResult.<BlogUser>success("登陆成功").data(blogUser).build();
    }

    @Override
    public BaseResult<BlogUser> register(BlogUser blogUser) {
        if (blogUser == null || StringUtils.isBlank(blogUser.getName())) {
            return BaseResult.<BlogUser>error("名称不能为空").build();
        }
        if (StringUtils.isBlank(blogUser.getId())) {
            blogUser.setId(StringUtils.getUUID32());
        }
        blogUser.setStatus(CommonStatusEnum.NORMAL.getValue());
        blogUser.setToken(StringUtils.getUUID32());
        blogUser.setLastLoginTime(new Date());
        blogUser.setCreateTime(new Date());
        blogUser.setModifyTime(new Date());
        blogUserMapper.insert(blogUser);
        jedisClient.go(cluster -> cluster.set(AppGlobalConstants.getTokenRedisKey(blogUser.getToken()), JSON.toJSONString(blogUser)));
        return BaseResult.<BlogUser>success().data(blogUser).build();
    }

    @Override
    public BaseResult<BlogUser> modifyUser(BlogUser blogUser) {
        if (StringUtils.isBlank(blogUser.getId())) {
            return BaseResult.<BlogUser>error("ID不能为空").build();
        }
        BlogUser user = getById(blogUser.getId());
        if (user != null) {
            blogUser.setModifyTime(new Date());
            updateById(blogUser);
            jedisClient.go(cluster -> cluster.set(AppGlobalConstants.getTokenRedisKey(blogUser.getToken()), JSON.toJSONString(blogUser)));
            return BaseResult.<BlogUser>success().data(blogUser).build();
        }
        return BaseResult.<BlogUser>error("用户不存在").build();
    }

    @Override
    public BaseResult<BlogUser> modifyUserByAgent(BlogUserAgent blogUserAgent) {
        if (blogUserAgent == null || StringUtils.isBlank(blogUserAgent.getId())) {
            return BaseResult.<BlogUser>error("ID不能为空").build();
        }
        BlogUser user = getById(blogUserAgent.getId());
        if (user != null) {
            user.setName(blogUserAgent.getName());
            user.setEmail(blogUserAgent.getEmail());
            user.setAvatarUrl(blogUserAgent.getAvatarUrl());
            user.setModifyTime(new Date());
            blogUserMapper.updateById(user);
            jedisClient.go(cluster -> cluster.set(AppGlobalConstants.getTokenRedisKey(user.getToken()), JSON.toJSONString(user)));
            return BaseResult.<BlogUser>success().data(user).build();
        }
        return BaseResult.<BlogUser>error("用户不存在").build();
    }
}
